package com.efast.cafe.portal.service.statemach.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.commponent.statemach.bean.StateMachByRoleBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.statemach.PortalStateMachRelBean;
import com.efast.cafe.portal.bean.statemach.PortalStateMachState;
import com.efast.cafe.portal.bean.statemach.PortalStateMachStateBean;
import com.efast.cafe.portal.bean.statemach.PortalStateMachStateConf;
import com.efast.cafe.portal.bean.statemach.PortalStateMachStateQueryBean;
import com.efast.cafe.portal.dao.statemach.PortalStateMachStateDao;
import com.efast.cafe.portal.entity.company.PortalCompanyRole;
import com.efast.cafe.portal.service.statemach.IPortalStateMachRelService;
import com.efast.cafe.portal.service.statemach.IPortalStateMachStateService;

@Service
@Transactional
public class PortalStateMachStateServiceImpl implements IPortalStateMachStateService {

	@Autowired
	private PortalStateMachStateDao portalStateMachStateDao;

	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;

	@Autowired
	private IPortalStateMachRelService portalStateMachRelService;

	@Override
	public Long insertState(PortalStateMachState state) {
		portalStateMachStateDao.insertState(state);
		return state.getId();
	}
	
	@Override
	public void insertStateConf(PortalStateMachStateConf stateConf) {
		portalStateMachStateDao.insertStateConf(stateConf);
	}
	
	@Override
	public void replaceStateConf(PortalStateMachStateConf stateConf) {
		portalStateMachStateDao.replaceStateConf(stateConf);
	}
	
	@Override
	public void updateState(PortalStateMachState state) {
		portalStateMachStateDao.updateState(state);
		// 查找受影响的confId清除redis缓存
		List<PortalStateMachRelBean> relList = portalStateMachRelService.queryRelListByPara(null, state.getId());
		for (PortalStateMachRelBean rel : relList) {
			objectRedisTemplate.delete(CommonConstant.REDIS_STATEMACH_CONFID_ + rel.getConfId());
		}
	}

	@Override
	public void deleteState(PortalStateMachState state) {
		portalStateMachStateDao.deleteState(state);
		// 查找受影响的confId清除redis缓存
		List<PortalStateMachRelBean> relList = portalStateMachRelService.queryRelListByPara(null, state.getId());
		for (PortalStateMachRelBean rel : relList) {
			objectRedisTemplate.delete(CommonConstant.REDIS_STATEMACH_CONFID_ + rel.getConfId());
		}
	}

	@Override
	public PortalStateMachStateQueryBean queryPageStateMachState(PortalStateMachStateQueryBean queryBean) {
		queryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		queryBean.setResultList(portalStateMachStateDao.queryStatePageList(queryBean));
		queryBean.setTotalRows(portalStateMachStateDao.queryStatePageCount(queryBean));
		return queryBean;
	}

	@Override
	public List<PortalStateMachStateBean> getPortalStateMachStateBeanByConfId(Long ConfId) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<StateMachByRoleBean> getPortalStateMachStateByRole(List<PortalCompanyRole> roles, String companyCode) {
		return portalStateMachStateDao.getPortalStateMachStateByRole(roles, companyCode);
	}

	public List<StateMachByRoleBean> getPortalStateMachStateByRoleForAdmin(String companyCode) {
		return portalStateMachStateDao.getPortalStateMachStateByRoleForAdmin(companyCode);
	}

	/**
	 * 根据企业返回状态定义
	 * 
	 * @param companyCode
	 * @return
	 */
	public List<PortalStateMachStateBean> getPortalStateMachStateByCompanyCode(String companyCode) {
		return portalStateMachStateDao.getPortalStateMachStateByCompanyCode(companyCode);
	}

	/**
	 * 根据企业名获取可配置状态
	 */
	@Override
	public List<PortalStateMachState> queryCanConfigStateByCompanyCode(String companyCode) {
		return portalStateMachStateDao.queryCanConfigStateByCompanyCode(companyCode);
	}

}
